package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author benben278
 * @create 2022-03-11 14:41
 */
@RestController
@RequestMapping("/checkitem") //checkitem/add
public class CheckItemController {
    @Reference //在zookeepper注册中心查找服务，《前面一直出错就是这里导包导错了》
    private CheckItemService checkItemService;
    //新增检查项
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){ //页面提交过来的是json格式的数据，默认情况下springMVC是无法解析和封装的，必须加一个@RequestBody注解才能进行封装和解析
        try{  //采用这种办法判断是否保存成功
            checkItemService.add(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //说明服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }
    //检查项分页查询
    @RequestMapping("/findpage")
    public PageResult findpage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkItemService.pageQuery(queryPageBean);
        return pageResult;
    }
    //删除检查项
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{  //采用这种办法判断是否保存成功
            checkItemService.deleteById(id);
        }catch (Exception e){
           // e.printStackTrace();
            //说明服务调用失败 {"data":null,"flag":false,"message":"删除检查项失败"}
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
    //编辑检查项
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try{  //采用这种办法判断是否保存成功
            checkItemService.edit(checkItem);
        }catch (Exception e){
            // e.printStackTrace();
            //说明服务调用失败 {"data":null,"flag":false,"message":"删除检查项失败"}
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
    //回显检查项数据
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{  //采用这种办法判断是否保存成功
           CheckItem checkItem=checkItemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
            // e.printStackTrace();
            //说明服务调用失败 {"data":null,"flag":false,"message":"删除检查项失败"}
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    //回显检查项数据
    @RequestMapping("/findAll")
    public Result findAll(){
        try{  //采用这种办法判断是否保存成功
            List<CheckItem> list =checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e){
            // e.printStackTrace();
            //说明服务调用失败 {"data":null,"flag":false,"message":"删除检查项失败"}
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
